package com.jhte.scheduleJob.base.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author wuchun
 * @description 线程安全的日期转换
 * @create 2017/12/28 10:11
 */
public class ConcurrentDateUtil {

    private static ThreadLocal<DateFormat> threadLocal = new ThreadLocal<>();

    private static Calendar calendar = Calendar.getInstance();

    /**
     * 日期转换
     *
     * @param date_format
     * @return
     */
    public static DateFormat getDateFormat(String date_format) {
        DateFormat df = threadLocal.get();
        if (df == null) {
            df = new SimpleDateFormat(date_format);
            threadLocal.set(df);
        }
        return df;
    }

    public static DateFormat getDateFormat() {
        DateFormat df = threadLocal.get();
        if (df == null) {
            df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            threadLocal.set(df);
        }
        return df;
    }

    public static String formatDate(Date date) throws ParseException {
        return getDateFormat().format(date);
    }

    public static Date parseStrDate(String strDate) throws ParseException {
        return getDateFormat().parse(strDate);
    }

    public static String formatDate(Date date, String date_format) throws ParseException {
        return getDateFormat(date_format).format(date);
    }

    public static Date parseStrDate(String strDate, String date_format) throws ParseException {
        return getDateFormat(date_format).parse(strDate);
    }

    /**
     * 获取日期间隔天数
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static long getIntervalDays(Date beginDate, Date endDate) {
        long beginTime = beginDate.getTime();
        long endTime = endDate.getTime();
        long betweenDays = (long) ((endTime - beginTime) / (1000 * 60 * 60 * 24) + 0.5);
        return betweenDays;
    }

    public static long getIntervalDays(String beginDateStr, String endDateStr) throws ParseException {
        Date beginDate = getDateFormat().parse(beginDateStr);
        Date endDate = getDateFormat().parse(endDateStr);
        return getIntervalDays(beginDate, endDate);
    }

    public static long getIntervalDays(String beginDateStr, String endDateStr, String date_format) throws ParseException {
        Date beginDate = getDateFormat(date_format).parse(beginDateStr);
        Date endDate = getDateFormat(date_format).parse(endDateStr);
        return getIntervalDays(beginDate, endDate);
    }

    /**
     * 日期加减
     *
     * @param beginDate
     * @param addDays
     * @return
     */
    public static Date getAddedDate(Date beginDate, long addDays) {
        calendar.setTime(beginDate);
        calendar.add(Calendar.DAY_OF_YEAR, (int) addDays);
        return calendar.getTime();
    }

    public static String getAddedDate(String beginDateStr, long addDays) throws ParseException {
        Date beginDate = getDateFormat().parse(beginDateStr);
        calendar.setTime(beginDate);
        calendar.add(Calendar.DAY_OF_YEAR, (int) addDays);
        return formatDate(calendar.getTime());
    }

    public static String getAddedDate(String beginDateStr, long addDays, String date_format) throws ParseException {
        Date beginDate = getDateFormat(date_format).parse(beginDateStr);
        calendar.setTime(beginDate);
        calendar.add(Calendar.DAY_OF_YEAR, (int) addDays);
        //System.out.println(calendar.getTime());
        return formatDate(calendar.getTime(), date_format);
    }

    /*public static void main(String[] args) throws ParseException {
        String str = getAddedDate("2017-12-17",32,"yyyy-MM-dd");
        System.out.println(str);
    }*/

}
